#region Using Statements

using System;
using System.Collections.Generic;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Text;
using Dotss.Core;

#endregion

namespace Dotss.Client
{

	/// <summary>
	/// 
	/// </summary>
	/// <remarks>NOT COMPLETED...  EVENTUALLY SHOULD BE EXPOSED AS PUBLIC</remarks>
	internal class RemotingServerLocator : ServerLocator
	{

		#region Fields

		private static TcpChannel _ServerChannel;
		private readonly static object _ChannelLock = new object();
		private ClientConfig _Config;

		#endregion

		private void CreateChannel()
		{

			if (_ServerChannel == null)
			{
				lock (_ChannelLock)
				{
					if (_ServerChannel == null)
					{

						_ServerChannel = new TcpChannel(ServerConfig.STATE_PORT);
						ChannelServices.RegisterChannel(_ServerChannel, false);

					}
				}
			}

		}

		private void DestroyChannel()
		{

			if (_ServerChannel != null)
			{
				lock (_ChannelLock)
				{
					if (_ServerChannel != null)
					{
						ChannelServices.UnregisterChannel(_ServerChannel);
						_ServerChannel = null;
					}
				}
			}

		}

		public ServerRO GetServer(ClientConfig config)
		{

			if (_Config == null) _Config = config;

			string serverAddress = string.Format("tcp://{0}:{1}", ClientConfig.Settings.Servers[0].Address, ServerConfig.STATE_PORT);
			return (ServerRO)Activator.GetObject(typeof(ServerRO), serverAddress);
		
		}

	}

}
